package com.shensi.adminCollect.controller.label;

import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.adminCollect.common.exception.CustomException;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.domain.label.ButtonSwitch;
import com.shensi.adminCollect.service.label.IButtonSwitchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;
@RestController
@RequestMapping("/button")
@Api(value = "按钮开关")
@Slf4j
public class ButtonSwitchController {

    @Autowired
    private IButtonSwitchService buttonSwitchService;

    @GetMapping("/switch")
    @ApiOperation(value = "查询按钮信息",response = ButtonSwitch.class,httpMethod = "GET")
    public AjaxResult getButtonSwitch(@RequestParam("userId") Long userId){
        LoginUser current = SecurityUtils.getLoginUser();
        SysUser sysUser = current.getUser();
        if(Objects.isNull(sysUser)){
            return AjaxResult.error("查询按钮信息，用户不存在");
        }

        ButtonSwitch buttonSwitch = buttonSwitchService.getButtonSwitch(sysUser);
        if(Objects.isNull(buttonSwitch)){
            return AjaxResult.error("获取不到按钮开关信息");
        }
        return AjaxResult.success(buttonSwitch);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改按钮信息",httpMethod = "POST")
    public AjaxResult updateButtonSwitch(@RequestBody ButtonSwitch buttonSwitch){
        //只有油企才可以设置按钮
        LoginUser current = SecurityUtils.getLoginUser();
        SysUser sysUser = current.getUser();
        if(Objects.isNull(sysUser)){
            return AjaxResult.error("修改按钮信息，用户不存在");
        }

        if(StringUtils.isNotEmpty(sysUser.getUserLevel()) && !UserConstants.LEVEL_ONE.equals(sysUser.getUserLevel())
                && !UserConstants.LEVEL_TWO.equals(sysUser.getUserLevel())){
            return AjaxResult.error("修改按钮信息，用户权限不匹配");
        }
        if(Objects.isNull(buttonSwitch) || Objects.isNull(buttonSwitch.getId())){
            throw new CustomException("updateButtonSwitch请求参数异常");
        }
        int num = buttonSwitchService.updateButtonSwitch(buttonSwitch);
        if(num <= 0){
            return AjaxResult.error("按钮开关信息更新失败");
        }
        return AjaxResult.success(num);
    }
}
